<template>
	<view class="xinren">
		<view class="xinrentou">
			<view class="" style="width: 100%;height: 460rpx;">

			</view>
			<view class="xinshoutubiao">

				<view class="neirong">
					<image class="iocimg" src="../static/qiandao/xinrenhb.png" mode=""></image>
					<view class="chazhiname">
						新人红包
					</view>
				</view>
				<view class="neirong">
					<image class="iocimg" src="../static/qiandao/limasong.png" mode=""></image>
					<view class="chazhiname">
						30分钟送达
					</view>
				</view>
				<view class="neirong">
					<image class="iocimg" src="../static/qiandao/tuijianyl.png" mode=""></image>
					<view class="chazhiname">
						推荐有礼
					</view>
				</view>
				<view class="neirong">
					<image class="iocimg" src="../static/qiandao/zhekou.png" mode=""></image>
					<view class="chazhiname">
						超值折扣
					</view>
				</view>
				<view class="neirong">
					<image class="iocimg" src="../static/qiandao/shouhouwy.png" mode=""></image>
					<view class="chazhiname">
						售后无忧
					</view>
				</view>
			</view>
			<view class="shangpin">
				<view class="" style="width: 100%;height: 32rpx;">

				</view>

				<view class="shangpindan" v-for="(item, index) in tlist">
					<image class="leftimg" :src="item.img" mode=""></image>
					<view class="fithrtext">
						<view class="fithrtextleft">
							<view class="fithrtextlefttop">
								{{ item.title }}
							</view>
							<view class="fithrtextleftzhong">
								<text style="color: #E04C20;font-size: 24rpx;margin-right: 12rpx;">抢购价</text>
								<text style="color: #E04C20; font-size: 24rpx;">￥</text>
								<text style="color: #E04C20;font-size: 48rpx;font-weight: 600;">0.01</text>
								<text style="color: #999999;font-size: 24rpx;margin-left: 12rpx;">¥{{ item.stockDto.price / 100 }}</text>
							</view>
							<view class="fithrtextleann">
								已抢{{ item.useNum }}份
							</view>
						</view>
						<image v-show="item.isUse" @click.stop="addCartb" src="https://laileme123.oss-cn-shanghai.aliyuncs.com/bg/20210113211110.png"
						 mode="" class="fithrtextright"></image>
						<image v-show="!item.isUse" @click.stop="addCart(index)" class="fithrtextright" src="../static/qiandao/qingshangp.png"
						 mode="" @click=""></image>
					</view>
					<!-- 如果已经买完 -->
					<image v-if="!item.stockDto.sales" class="dingweiimg" src="https://laileme123.oss-cn-shanghai.aliyuncs.com/bg/bc4b557247ec4572bb4c2ed3400b2d2c.png"
					 mode=""></image>
				</view>
				<view class="" style="width: 100%;height: 200rpx;">

				</view>

			</view>
		<!-- 	<view class="dibuanniu">
				<image @click="navToCart" class="dingweiimg" src="https://laileme123.oss-cn-shanghai.aliyuncs.com/bg/e8eea87014d547bc852805eb3f32c6fa.png"
				 mode=""></image>

			</view> -->
		</view>
		<bottom v-model="nums"></bottom>
	</view>
</template>

<script>
		import bottom from '../discount/components/bottom.vue'
			import mixi from '../discount/data/index.js'
	export default {
			mixins:[mixi],
		data() {
			return {
		
				recommendType: 2,
				tlist: [],
				pageNo: 1,
				number: 0,
				isnumber: 0,
				cartNum: 0,
				cartNums: 0,
				yaoqingma: '',
			}
		},
		components: {
			bottom
		},
		onLoad(option) {


		},
		onShow() {
			this.gethuoqup()
			this.getRecommendByStorage('refresh')
			this.getUserCoupons();
		},
		onShareAppMessage() {
			console.log('this.yaoqingma', this.yaoqingma)
			return {
				title: '新人专享1分福利-新鲜食材，30分钟送达',
				// imageUrl: this.goods.img,
				path: '/pages/share/inviteNewPeople?p=' + this.yaoqingma,
			};
		},

		methods: {
			//获取p
			gethuoqup() {
				let that = this;
				that.$api
					.request('activity', 'inviteBuyingInfo', failres => {
						that.logining = false;
						that.$api.msg(failres.errmsg);
					})
					.then(res => {

						that.yaoqingma = res.data.p
						console.log('获取p', that.yaoqingma)
					});
			},
			//去购物车
			navToCart() {
				uni.switchTab({
					url: `/pages/cart/cart`
				});
			},
			addCart(index) {

				console.log();
				const that = this;
				console.log('判断抢购卷', this.number, this.isnumber);
				if (this.tlist[index].stockDto.sales == 0) {
					that.$api.msg('抢购卷已用光,或商品数量不足');
					return;
				}

				if (this.number >= this.isnumber) {
					that.$api.msg('抢购卷已用光,或商品数量不足');
					return;
				}
				if (this.isnumber == 0) {
					that.$api.msg('抢购卷已用光,或商品数量不足');
					return;
				}



				that.$api
					.request('cart', 'addCartItem', {
						skuId: that.tlist[index].skuId,
						activityId: that.tlist[index].activityId,
						couponId: that.tlist[index].couponId,
						num: 1
					})
					.then(res => {
						that.$api.msg('添加购物车成功');
						this.countTabNum()
						var cartNum = this.$store.state.cartNum + 1;
						that.tlist[index].isUse = 1;
						let list = that.tlist;
						that.tlist = [];
						that.tlist.push(...list);
						that.tlist[index].num++;
						that.number++;
						that.cartNumFn();
						that.$store.commit('addCart', cartNum);
						uni.setTabBarBadge({
							index: 2,
							text: cartNum + ''
						});
					});
			},
			cartNumFn() {
				let cartNum = 0;
				this.tlist.forEach((item, i) => {
					cartNum += item.num;
				});
				this.cartNums = cartNum;
			},
			addCartb() {
				this.$api.msg('抢购卷已用光');
				return;
			},
			getUserCoupons() {
				let that = this;
				this.$api
					.request(
						'coupon',
						'getUserCoupons', {
							couponType: 2,
							status: 0
						},
						failres => {
							this.logining = false;
							this.$api.msg(failres.errmsg);
						}
					)
					.then(res => {
						// debugger;
						this.isnumber = res.data.length;
						this.CouponsList = res.data;
					});
			},
			//获取商品数据
			getRecommendByStorage(loadMore = '') {
				let that = this;
				console.log(this.recommendType, 'this.recommendType');
				if (loadMore === 'refresh') {
					this.pageNo = 1;
				}
				if(this.$store.state.storageId==10){
					that.$api
						.request(
							'activity',
							'activityGoodsList', {
								activityType: 2,
								storageId: 26,
								pageNo: this.pageNo
							},
							failres => {
								that.logining = false;
								that.$api.msg(failres.errmsg);
							}
						)
						.then(res => {
							that.pageNo++;
							res.data.forEach(item => {
								that.tlist.push(item);
							});
							for (let i = 0; i < that.tlist.length; i++) {
								that.tlist[i].isUse = 0;
								if (i == that.tlist.length - 1) {
									that.loadData();
								}
							}
					
						});
						return
					
				}
				if (this.$store.state.storageId) {
					that.$api
						.request(
							'activity',
							'activityGoodsList', {
								activityType: 2,
								storageId: this.$store.state.storageId,
								pageNo: this.pageNo
							},
							failres => {
								that.logining = false;
								that.$api.msg(failres.errmsg);
							}
						)
						.then(res => {
							that.pageNo++;
							res.data.forEach(item => {
								that.tlist.push(item);
							});
							for (let i = 0; i < that.tlist.length; i++) {
								that.tlist[i].isUse = 0;
								if (i == that.tlist.length - 1) {
									that.loadData();
								}
							}

						});
				} else {
					that.$api
						.request(
							'activity',
							'activityGoodsList', {
								activityType: 2,
								storageId: 26,
								pageNo: this.pageNo
							},
							failres => {
								that.logining = false;
								that.$api.msg(failres.errmsg);
							}
						)
						.then(res => {
							that.pageNo++;
							res.data.forEach(item => {
								that.tlist.push(item);
							});
							for (let i = 0; i < that.tlist.length; i++) {
								that.tlist[i].isUse = 0;
								if (i == that.tlist.length - 1) {
									that.loadData();
								}
							}

						});
				}


			},
			//请求数据
			loadData() {
				const that = this;
				that.$api
					.request('cart', 'getCartList', {
						storageId: this.$store.state.storageId
					})
					.then(res => {
						//修改为for i循环，以找出库存不足的商品索引并去掉，存入库存不足商品组
						// var stockless = [];//库存不足商品索引
						var lessIds = [];
						for (var i = 0; i < res.data.length; i++) {
							console.log(res.data[i].id);
							for (let j = 0; j < that.tlist.length; j++) {
								console.log(res.data[i].couponId, that.tlist[j].couponId);
								console.log(res.data[i].skuId === that.tlist[j].skuId);
								if (res.data[i].couponId === that.tlist[j].couponId && res.data[i].skuId === that.tlist[j].skuId) {
									that.tlist[j].isUse = 1;
									that.number++;
									console.log(i);
								}
							}
						}

						that.cartList = res.data;

					});
			},
			//已经领取
			yijinglu() {
				uni.showToast({
					title: '已经领取了！',
					icon: 'none'
				});
			},
			//立即领取
			lijilingqu(item) {
				console.log('立即领取', item)
				if (item.min > this.userxinxi.signMoney) {
					uni.showToast({
						title: '积分不足！',
						icon: 'none'
					});
					return
				}

				var that = this
				var obj = {}
				obj.couponId = item.id
				that.$api
					.request('coupon', 'giveUserSignCoupons', obj, failres => {
						that.$api.msg(failres.errmsg);
						uni.hideLoading();
					})
					.then(res => {

						if (res.errno == '200') {
							that.$api.msg(res.errmsg);
							that.getalljuan()

						}
					});
			},
			//获取用户信息--签到次数
			getqiandao() {
				var that = this
				let obj = {};
				obj.userId = uni.getStorageSync('userInfo').id;
				that.$api
					.request('user', 'getUserInfo', obj, failres => {
						that.$api.msg(failres.errmsg);
						uni.hideLoading();
					})
					.then(res => {
						if (res.errno == '200') {
							if (res.data) {
								that.userxinxi = res.data
								console.log('that.userxinxi', that.userxinxi)
							}
						}
					});
			},
			//时间转换
			zhuanhaun(date) {
				var date = new Date(date);
				var YY = date.getFullYear() + '-';
				var MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
				var DD = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());
				var hh = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
				var mm = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
				var ss = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
				return YY + MM + DD + " " + hh + mm + ss;
			},

			//获取用户签到收益记录
			getalljuan() {
				var that = this
				that.$api
					.request('coupon', 'getSignCouponsList', {}, failres => {
						that.$api.msg(failres.errmsg);
						uni.hideLoading();
					})
					.then(res => {

						if (res.errno == '200') {

							that.duihuanfuliList = res.data
						}
					});
			},
		}
	}
</script>

<style lang="scss">
	page {

		background: linear-gradient(180deg, #FFA05B 0%, #FF2E2E 100%);
	}

	.xinren {
		.dibuanniu {
			width: 750rpx;
			height: 188rpx;
			background: #FFFFFF;
			opacity: 0.9;
			position: fixed;
			left: 0;
			bottom: 0;
			display: flex;
			align-items: center;
			justify-content: center;

			.dingweiimg {
				width: 702rpx;
				height: 88rpx;
			}


		}

		.shangpin {
			width: 702rpx;
			background-color: #FFEFE3;
			margin-left: 24rpx;
			margin-top: 143rpx;
			padding-bottom: 20rpx;

			.shangpindan {
				width: 670rpx;
				height: 216rpx;
				margin-left: 16rpx;
				margin-bottom: 16rpx;
				background: url('https://laileme123.oss-cn-shanghai.aliyuncs.com/bg/7d6ec253b2a9444db7256051e30a914e.png');
				background-size: 100% 100%;
				display: flex;
				align-items: center;
				position: relative;

				.dingweiimg {
					width: 670rpx;
					height: 216rpx;
					position: absolute;
					top: 0;
					left: 0;
				}

				.fithrtext {
					width: 422rpx;
					margin-left: 24rpx;
					display: flex;
					justify-content: space-between;
					align-items: center;

					.fithrtextleft {

						.fithrtextleann {
							width: 118rpx;
							height: 28rpx;
							background: #36B542;
							border-radius: 16rpx;
							font-size: 20rpx;
							font-family: PingFangSC-Regular, PingFang SC;
							font-weight: 400;
							color: #FFFFFF;
							display: flex;
							align-items: center;
							justify-content: center;
							margin-top: 20rpx;
						}

						.fithrtextleftzhong {
							height: 48rpx;
							margin-top: 15rpx;
						}

						.fithrtextlefttop {
							width: 300rpx;
							height: 40rpx;
							font-size: 32rpx;
							font-family: PingFangSC-Regular, PingFang SC;
							font-weight: 400;
							color: #333333;
							margin-top: 20rpx;
							overflow: hidden;
						}
					}

					.fithrtextright {
						width: 84rpx;
						height: 90rpx;
					}

				}

				.leftimg {
					width: 200rpx;
					height: 200rpx;
					margin-left: 8rpx;
				}
			}

		}

		.xinshoutubiao {
			width: 750rpx;
			height: 100rpx;
			padding-left: 54rpx;
			padding-right: 54rpx;

			display: flex;
			align-items: center;
			justify-content: space-between;

			.neirong {
				text-align: center;

				.chazhiname {
					font-size: 24rpx;
					font-family: PingFangSC-Regular, PingFang SC;
					font-weight: 400;
					color: #FFFFFF;
				}

				.iocimg {
					width: 56rpx;
					height: 56rpx;
				}

			}

		}

		.xinrentou {
			margin-top: 48rpx;
			width: 750rpx;
			height: 702rpx;
			background: url('https://laileme123.oss-cn-shanghai.aliyuncs.com/bg/e04319da8801444e8f38425b7d6df4bd.png');
			background-size: 100% 100%;
		}
	}
</style>
